/**
 * <p>原型管理器</p>
 * <p>实现了自动注册原型功能，采用单例模式<p>
 * @author MIAO
 * Created on 2011-10-20
 * For more information, please visit:ismiao.com
 */
package com.ismiao.designPattern.Prototype;

import java.util.HashMap;
import java.util.Map;

public class PrototypeManager {
	private Map<String, Object> map = null;
	private static PrototypeManager prototypeManager = null;

	private PrototypeManager() {
		map = new HashMap<String, Object>();
	}

	public static synchronized PrototypeManager getPrototypeManager() {
		if (prototypeManager == null)
			prototypeManager = new PrototypeManager();
		return prototypeManager;
	}

	public void register(String name, Object o) {
		map.put(name, o);
	}

	public void unRegister(String name) {
		map.remove(name);
	}

	public Object getPrototype(String name) {
		Object o = null;
		if (map.containsKey(name))
			return map.get(name);
		try {
			o = Class.forName(name).newInstance();
			register(name, o);
		} catch (Exception e) {
			System.out.println("Class " + name + " not defined!");
		}
		return o;
	}
}
